﻿using System;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.Services;
using BluePortal.Medias.Core;

namespace BluePortal.QuikStart.Images
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ChannelHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["chid"] != null)
            {
                try
                {
                    context.Response.ContentType = "image/jpeg";
                    Guid chid = new Guid(context.Request["chid"]);
                    Channel c = new BCChannel().FindByID(chid);

                    MemoryStream memory = new MemoryStream(c.ChannelLogo);
                    Image bm = Bitmap.FromStream(memory);
                    context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);   
                    bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                catch
                {
                    context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    Image bmnull = Bitmap.FromFile(context.Server.MapPath("~/Images/anonymous.jpg"));
                    bmnull.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
